package heip.generator.main;

import heip.generator.entity.EntityClazz;
import heip.generator.entity.EntityTable;
import heip.generator.generer.ActionGenerator;
import heip.generator.generer.EntityGenerator;
import heip.generator.generer.JSPGenerator;
import heip.generator.generer.ServiceGenerator;


/**
 * 三层表(走临时表)的代码生成器
 * 
 *1)修改模块moduleName
 *2）
 *a)生成表的实体对象
 * 注释main方法里里的gg.generatorClazzAndJSP(),启用gg.generatorTable()
 * 修改generatorTable 里的变量，参考方法说明
 *b)生成 service,action,jsp(必须先执行生成实体，并已经拷贝到依赖的开发工程里)
 * 注释main方法里里的gg.generatorTable(),启用gg.generatorClazzAndJSP()
 * 修改 generatorClazzAndJSP 里的变量，参考方法说明
 * 
 * @author HSWHM
 * Modify By:<br/>
 * Modify Date:2012-7-20<br/>
 * Remark:修改说明<br/>
 */
public class ThreeTableCodeGenerator  extends BaseAbstractCodeGenerator{
	/**
	 * 模块包名
	 */
	private String moduleName = "tobacco";
	
	/**
	 * 生成实体使用说明
	 * 1)定义表对象
	 * 例如： 
	 * EntityTable sub1 = new EntityTable("TC_PICK_IN_STOCK_PLACE(孙表名)", "IN_DTL_ID(表对应儿表外键字段)");
	 * EntityTable sub = new EntityTable("TC_PICK_IN_STOCK_DTL(儿表名)", "IN_STOCK_ID(主表外键)",sub1(可多个));
	 * EntityTable mainTable = new EntityTable("TC_PICK_IN_STOCK(主表名)",sub(可多个));	
	 * 
	 * 2)调用gt(主表EntityTable)生成文件
	 */
	public void generatorTable() throws Exception {
		EntityTable sub1 = new EntityTable("TC_PICK_IN_STOCK_PLACE", "IN_DTL_ID");
		EntityTable sub = new EntityTable("TC_PICK_IN_STOCK_DTL", "IN_STOCK_ID",sub1);
		EntityTable mainTable = new EntityTable("TC_PICK_IN_STOCK",sub);	
		gt(mainTable);
	}
	
	/**
	 * 生成servcie,action和jsp使用说明
	 * 1)定义对象
	 * 例如： 参数：子表类Class,实体中文名称，外键属性名
	 * EntityClazz ss = new EntityClazz(TcPickInStockPlace.class,"挑选入库库存明细","inDtlId");
	 * EntityClazz sub = new EntityClazz(TcPickInStockDtl.class, "挑选入库烟叶信息","inStockId",ss);
	 * EntityClazz mainClazz = new EntityClazz(TcPickInStock.class,"挑选入库",sub);
	 * 
	 * 2)调用gt(主表EntityClazz)生成文件
	 */
	public void generatorClazzAndJSP()throws Exception{
		/*EntityClazz ss = new EntityClazz(TcPickInStockPlace.class,"挑选入库库存明细","inDtlId");
		EntityClazz sub = new EntityClazz(TcPickInStockDtl.class, "挑选入库烟叶信息","inStockId",ss);
		EntityClazz mainClazz = new EntityClazz(TcPickInStock.class,"挑选入库",sub);
		gc(mainClazz);*/
	}
	
	
	public static void main(String[] args)throws Exception{
		ThreeTableCodeGenerator three = new ThreeTableCodeGenerator();
//		three.generatorTable();
		three.generatorClazzAndJSP();
		open();
	}
	
	
	////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////
	private void gt(EntityTable table) throws Exception{
		EntityGenerator eg = new EntityGenerator(moduleName);
		eg.generatorPO(new EntityTable[]{table}, null);
		eg.generatorVO(new EntityTable[]{table}, false);
		
		for(EntityTable et : table.getSubEntitys()){
			//第二层表无子表直接grid
			if(!et.hasSubTable()){
				eg.generatorPO(new EntityTable[]{et}, null);
				eg.generatorVO(new EntityTable[]{et}, false);
				continue;
			}
			
			//临时表
			eg.generatorPO(new EntityTable[]{et}, null);
			eg.generatorVO(new EntityTable[]{et}, true);
			if(et.hasSubTable()){
				for(EntityTable ett :et.getSubEntitys()){
					eg.generatorPO(new EntityTable[]{ett}, null);
					eg.generatorVO(new EntityTable[]{ett}, true);
				}
			}
		}
	}
	
	private void gc(EntityClazz clazz)throws Exception{
		ServiceGenerator sg = new ServiceGenerator(moduleName);
		sg.generatorThree(clazz);
		ActionGenerator ag = new ActionGenerator(moduleName);
		ag.generateThree(clazz);
		JSPGenerator jg = new JSPGenerator(moduleName);
		jg.generateThree(clazz);
	}
}